﻿package com.victim.snake.snake{
	import flash.events.EventDispatcher;
	import flash.utils.Dictionary;
	import com.victim.snake.util.Loader;
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.display.LoaderInfo;
	import com.victim.snake.user.ProcessMsg;
	import flash.utils.ByteArray;

	public class SnakeStyle extends EventDispatcher {
		static public var snakeStyles:Dictionary=new Dictionary();//蛇样式列表
		
		public var headImage:Bitmap=null;//蛇头
		public var tailImage:Bitmap=null;//蛇尾
		public var trunImage:Bitmap=null;//蛇转角身体
		public var bodyImageVector:Vector.<Bitmap>=new Vector.<Bitmap>  ;//蛇身体
		public var bigThumbnailsImage:Bitmap=null;//大缩略图
		public var smallThumbnailsImage:Bitmap=null;//小缩略图

		private var id:int=-1;// id
		private var name:String="";//蛇名
		private var width:int=20;// 每节身体宽度
		private var height:int=20;// 每节身体高度
		private var length:int=6;// 初始节数
		private var speedBonus:int=0;// 速度加成(蛇的速度加成由地图初使速度-房间速度加成-蛇样式速度加成-用户速度加成)
		private var growthBonus:int=0;// 蛇长度增长加成 (控制当吃到食物时蛇的增长节数)
		private var unInitSpeed:int=0;// 当蛇复活时将不重置蛇速度的次数
		private var unInitLength:int=0;// 当蛇复活时将不重置蛇节数的次数
		private var reviviscenceNum:int=0;// 复活次数(蛇的复活次数由地图设置的每条蛇复活次数+蛇样式复活次数+用户可复活次数)
		private var autoAddArmor:int=0;// 活达到一定时间增加一层护甲(单拉秒，死亡重新记时，0为不增加)
		private var autoGrowth:int=0;// 存活达到一定时间增加autoGrowthNum设置的节数(死亡重新记时，0为不增加)
		private var autoGrowthNum:int=0;
		private var timeOfCreation:String="";// 添加时间
		//下载状态
		private var loadStatus:int=0;//下载状态 0末加载 1下载中 2下载完成

		//取得蛇样式
		static public function getStyle(snakeName:String):SnakeStyle {
			//如果样式末下载
			if (SnakeStyle.snakeStyles[snakeName]==undefined) {
				SnakeStyle.snakeStyles[snakeName]=new SnakeStyle(snakeName);//下载该样式
			}
			return SnakeStyle.snakeStyles[snakeName];
		}

		//构造函数
		public function SnakeStyle(name:String) {
			this.name=name;
		}

		
		//开始下载样式资源
		public function loading():void {
			if (this.loadStatus==0) {//如果状态为末加载，就开始加载
				this.loadStatus=1;
				new Loader("image/snake/"+name+"/head.png",this.headImageLoaderEvent);//下载蛇头
				new Loader("image/snake/"+name+"/tail.png",this.tailImageLoaderEvent);//下载蛇尾
				new Loader("image/snake/"+name+"/body.png",this.bodyImageLoaderEvent);//下载蛇身体
				new Loader("image/snake/"+name+"/trun.png",this.trunImageLoaderEvent);//下载蛇转角身体
				new Loader("image/snake/"+name+"/bigThumbnails.png",this.bigThumbnailsImageLoaderEvent);//下载大缩略图
				new Loader("image/snake/"+name+"/smallThumbnails.png",this.smallThumbnailsImageLoaderEvent);//下载小缩略图
				this.propertyLoadingEvent();	//下载蛇样式属性;

				//添加每张图片下载完成监听
				this.addEventListener(SnakeStyleEvent.BigThumbnailsLoaded,imageLoaded);
				this.addEventListener(SnakeStyleEvent.BodyImageVectorLoaded,imageLoaded);
				this.addEventListener(SnakeStyleEvent.HeadImageLoaded,imageLoaded);
				this.addEventListener(SnakeStyleEvent.SmallThumbnailsLoaded,imageLoaded);
				this.addEventListener(SnakeStyleEvent.TailImageLoaded,imageLoaded);
				this.addEventListener(SnakeStyleEvent.TrunImageLoaded,imageLoaded);
				this.addEventListener(SnakeStyleEvent.PropertyLoaded,propertyLoaded);	//蛇样式属性下载完成监听;
			} else if (this.loadStatus==2) {//如果以加载完成，抛出所有资源加载完成事件
				this.isAllLoad();
			}
		}


		//所有图片及属性是否加载完成,如果加载完成,抛出所有图片下载完成事件
		public function isAllLoad():Boolean {
			//trace(this.headImage+"/"+ this.tailImage +"/"+ this.bodyImageVector.length);
			if (this.id>=0&&this.headImage!=null&&this.tailImage!=null&&this.trunImage!=null&&this.bigThumbnailsImage!=null&&this.smallThumbnailsImage!=null&&this.bodyImageVector.length>0) {
				this.loadStatus=2;
				this.dispatchLoadedEvent();
				return true;
			}
			return false;
		}

		//当本实例内有图片下载完成时被调用，该方法调用isAllLoad判断所有图片是否下载完成，如果下载完成,抛出所有图片下载完成事件
		private function imageLoaded(e:SnakeStyleEvent):void {
			this.removeEventListener(e.type,this.imageLoaded);
			this.isAllLoad();
		}

		//抛出所有资源下载完成事件的方法
		private function dispatchLoadedEvent() {
			var snakeStyleEvent:SnakeStyleEvent=new SnakeStyleEvent(SnakeStyleEvent.IsLoaded,this);
			this.dispatchEvent(snakeStyleEvent);
		}


		//下载属性
		private function propertyLoadingEvent():void {
			if(this.id<0){	//如果末加载属性,则发送加载消息给服务器
				ProcessMsg.socketSend(ProcessMsg.generateUTF8Msg(19,this.name));
			}else{	//抛出属性加载完成事件
				this.dispatchEvent(new SnakeStyleEvent(SnakeStyleEvent.PropertyLoaded,this));
			}

		}
		//下载属性完成
		private function  propertyLoaded(e:SnakeStyleEvent):void{
			this.isAllLoad();
			this.removeEventListener(e.type,propertyLoaded);
		}
		
		//设置样式属性
		public function setSnakeStyleProperty(id:int,name:String,width:int,height:int,length:int,speedBonus:int,growthBonus:int,unInitSpeed:int,unInitLength:int,reviviscenceNum:int,autoAddArmor:int,autoGrowth:int,autoGrowthNum:int,timeOfCreation:String) {
			if(this.id<0){
				this.id=id;
				this.name=name;
				this.width=width;
				this.height=height;
				this.length=length;
				this.speedBonus=speedBonus;
				this.growthBonus=growthBonus;
				this.unInitSpeed=unInitSpeed;
				this.unInitLength=unInitLength;
				this.reviviscenceNum=reviviscenceNum;
				this.autoAddArmor=autoAddArmor;
				this.autoGrowth=autoGrowth;
				this.autoGrowthNum=autoGrowthNum;
				this.timeOfCreation=timeOfCreation;
			}
			this.dispatchEvent(new SnakeStyleEvent(SnakeStyleEvent.PropertyLoaded,this));
		}


		//下载蛇头
		function headImageLoaderEvent(event:Event):void {
			var loaderInfo:LoaderInfo=LoaderInfo(event.target);
			this.headImage=Bitmap(loaderInfo.content);
			this.dispatchEvent(new SnakeStyleEvent(SnakeStyleEvent.HeadImageLoaded,this));
			//trace(1);
		}
		//下载蛇尾
		function tailImageLoaderEvent(event:Event):void {
			var loaderInfo:LoaderInfo=LoaderInfo(event.target);
			this.tailImage=Bitmap(loaderInfo.content);
			this.dispatchEvent(new SnakeStyleEvent(SnakeStyleEvent.TailImageLoaded,this));
			//trace(2);
		}
		//下载蛇身体
		function bodyImageLoaderEvent(event:Event):void {
			var loaderInfo:LoaderInfo=LoaderInfo(event.target);
			bodyImageVector[bodyImageVector.length]=Bitmap(loaderInfo.content);
			this.dispatchEvent(new SnakeStyleEvent(SnakeStyleEvent.BodyImageVectorLoaded,this));
			//trace(3);
		}
		//下载蛇转角身体
		function trunImageLoaderEvent(event:Event):void {
			var loaderInfo:LoaderInfo=LoaderInfo(event.target);
			this.trunImage=Bitmap(loaderInfo.content);
			this.dispatchEvent(new SnakeStyleEvent(SnakeStyleEvent.TrunImageLoaded,this));
			//trace(1);
		}
		//下载大缩略图
		function bigThumbnailsImageLoaderEvent(event:Event):void {
			var loaderInfo:LoaderInfo=LoaderInfo(event.target);
			this.bigThumbnailsImage=Bitmap(loaderInfo.content);
			this.dispatchEvent(new SnakeStyleEvent(SnakeStyleEvent.BigThumbnailsLoaded,this));
		}
		//下载小缩略图
		function smallThumbnailsImageLoaderEvent(event:Event):void {
			var loaderInfo:LoaderInfo=LoaderInfo(event.target);
			this.smallThumbnailsImage=Bitmap(loaderInfo.content);
			this.dispatchEvent(new SnakeStyleEvent(SnakeStyleEvent.SmallThumbnailsLoaded,this));
		}


		//get and set

		public function getName():String {
			return this.name;
		}

		public function setName(name:String):void {
			this.name=name;
		}



		public function getId():int {
			return id;
		}

		public function setId(id:int) {
			this.id=id;
		}



		public function getWidth():int {
			return width;
		}

		public function setWidth(width:int) {
			this.width=width;
		}

		public function getHeight():int {
			return height;
		}

		public function setHeight(height:int) {
			this.height=height;
		}

		public function getLength():int {
			return length;
		}

		public function setLength(length:int) {
			this.length=length;
		}

		public function getSpeedBonus():int {
			return speedBonus;
		}

		public function setSpeedBonus(speedBonus:int) {
			this.speedBonus=speedBonus;
		}

		public function getGrowthBonus():int {
			return growthBonus;
		}

		public function setGrowthBonus(growthBonus:int) {
			this.growthBonus=growthBonus;
		}

		public function getUnInitSpeed():int {
			return unInitSpeed;
		}

		public function setUnInitSpeed(unInitSpeed:int) {
			this.unInitSpeed=unInitSpeed;
		}

		public function getUnInitLength():int {
			return unInitLength;
		}

		public function setUnInitLength(unInitLength:int) {
			this.unInitLength=unInitLength;
		}

		public function getReviviscenceNum():int {
			return reviviscenceNum;
		}

		public function setReviviscenceNum(reviviscenceNum:int) {
			this.reviviscenceNum=reviviscenceNum;
		}

		public function getAutoAddArmor():int {
			return autoAddArmor;
		}

		public function setAutoAddArmor(autoAddArmor:int) {
			this.autoAddArmor=autoAddArmor;
		}

		public function getAutoGrowthNum():int {
			return autoGrowthNum;
		}

		public function setAutoGrowthNum(autoGrowthNum:int) {
			this.autoGrowthNum=autoGrowthNum;
		}

		public function getTimeOfCreation():String {
			return timeOfCreation;
		}

		public function setTimeOfCreation(timeOfCreation:String) {
			this.timeOfCreation=timeOfCreation;
		}

		public function getAutoGrowth():int {
			return autoGrowth;
		}

		public function setAutoGrowth(autoGrowth:int) {
			this.autoGrowth=autoGrowth;
		}
		
	}

}